package Panel;
/**
 * Created by win10 on 2017/6/12.
 */
import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.border.EmptyBorder;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

public class Demo extends JFrame{
    private JPanel contentPanel;
    private JMenuBar mBar;// 菜单
    private JToolBar bar;
    private JLabel statusLabel;
    private MainPanel mainPanel;
    public Demo(){
        this.setTitle("Panel(Demo)");
        this.contentPanel=new JPanel();
        this.contentPanel.setBorder(new EmptyBorder(5,5,5,5));
        this.contentPanel.setLayout(new BorderLayout(0,0));
        this.setContentPane(contentPanel);


        toolbarInitialization();
        menubarInitialization();
        StatusBar timeFrame = new StatusBar();

//----mousePosition
        statusLabel = new JLabel("prepareing",JLabel.CENTER);
        statusLabel.setSize(350,100);

//----

//----状态栏
        JPanel statusPanel=new JPanel();
        statusPanel.setLayout(new BoxLayout(statusPanel,BoxLayout.X_AXIS ));
        statusPanel.add(timeFrame);
        statusPanel.add(Box.createHorizontalGlue ());
        statusPanel.add(statusLabel);
//----End

        this.contentPanel.add(bar,BorderLayout.NORTH);
        this.contentPanel.add(statusPanel,BorderLayout.SOUTH);

        DrawPanel drawPanel = new DrawPanel();
       
        drawPanel.addMouseMotionListener(new CustomMouseMotionListener());
        drawPanel.showGui();

        this.contentPanel.add(drawPanel,BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 0, 500, 700);
        this.setVisible(true);
    }

    private void toolbarInitialization(){
        bar = new JToolBar();
        java.lang.String[] logo={"〇","△","□","-","+","移动图形","旋转图形","缩放图形","显示图形"};
        java.util.List<JButton> toolbarButtonList = new java.util.ArrayList();

        for(int i = 0; i < logo.length; ++i) {
            toolbarButtonList.add(new JButton(logo[i]));
        }

        for(JButton btn : toolbarButtonList ) {
            btn.addActionListener(new ClickAction());
            bar.add(btn);
        }

    }

    private void menubarInitialization() {
        JMenuBar mBar = new JMenuBar();
        this.setJMenuBar(mBar);
        java.lang.String[] iconSrc={"news","opens","save","exit"};
        java.lang.String[] menuName={"文件","编辑","画笔颜色","帮助","关于"};
        java.lang.String[] fileItemString={"新建","打开","保存","退出"};
        java.lang.String[] editItemString={"旋转","平移","放大","缩小"};
        java.lang.String[] colorString={"红色","黄色","绿色"};
        java.util.List<JMenu> menuButtonList = new java.util.ArrayList();
        java.util.List<JMenuItem> fileItem = new java.util.ArrayList();
        java.util.List<JMenuItem> editItem = new java.util.ArrayList();
        java.util.List<JMenuItem> colorSet = new java.util.ArrayList();

        menuBuilder(mBar, menuName, menuButtonList);
        menuItemBuilder(fileItemString, iconSrc,menuButtonList,0, fileItem);
        menuItemBuilder(editItemString, menuButtonList,1, editItem);
        menuItemBuilder(colorString, menuButtonList, 2, colorSet);


    }

    private void menuBuilder(JMenuBar mBar, String[] menuName, List<JMenu> menuButtonList) {
        //生成所有菜单栏选项
        for(int i = 0; i < menuName.length; ++i)
            menuButtonList.add(new JMenu(menuName[i]));
        //将加入到bar中
        for(JMenu menu : menuButtonList )
            mBar.add(menu);
    }

    private void menuItemBuilder(String[] editItemString, List<JMenu> menuButtonList,int position, List<JMenuItem> editItem) {
        //生成"编辑"下所有选项
        for(int i = 0; i < editItemString.length; ++i) {
            editItem.add(new JMenuItem(editItemString[i]));
        }
        //为"文件"下所有选项增加鼠标监听
        for(JMenuItem item : editItem ) {
            item.addActionListener(new ClickAction());
                menuButtonList.get(position).add(item);
        }
    }
    private void menuItemBuilder(String[] itemString,String[] iconSrc, List<JMenu> menuButtonList,int position, List<JMenuItem> itemList) {
        //带icon版本的menuItemBuilder
        for(int i = 0; i < itemString.length; ++i) {
            itemList.add(new JMenuItem(itemString[i],new ImageIcon("src/icon/"+iconSrc[i]+".png")));
        }

        for(JMenuItem item : itemList ) {
            item.addActionListener(new ClickAction());
            menuButtonList.get(position).add(item);
        }
    }



    public static void main(String[]args){
        Demo example=new Demo();
    }

    class CustomMouseMotionListener implements MouseMotionListener {
        public void mouseDragged(MouseEvent e) {
            statusLabel.setText("Mouse Dragged: ("+e.getX()+", "+e.getY() +")");
        }

        public void mouseMoved(MouseEvent e) {
            statusLabel.setText("Mouse Moved: ("+e.getX()+", "+e.getY() +")");
        }
    }
}


